package com.yang.job.admin.application.convertor;


import com.yang.job.admin.client.dto.YangJobDTO;
import com.yang.job.admin.domain.enums.JobModelFeatureEnum;
import com.yang.job.admin.domain.enums.JobTypeEnum;
import com.yang.job.admin.domain.model.YangJobModel;
import com.yang.job.admin.domain.valueobject.RemoteExecutorMessage;
import org.springframework.stereotype.Component;

@Component
public class YangJobDTOConvertor {
    public YangJobDTO convert2DTO(YangJobModel yangJobModel) {
        if (yangJobModel == null) {
            return null;
        }
        YangJobDTO yangJobDTO = new YangJobDTO();
        yangJobDTO.setJobId(yangJobModel.getJobId());
        yangJobDTO.setDescription(yangJobModel.getDescription());
        yangJobDTO.setJobName(yangJobModel.getJobName());
        yangJobDTO.setCron(yangJobModel.getCron());
        yangJobDTO.setEnable(yangJobModel.getEnable());
        yangJobDTO.setOpen(yangJobModel.getOpen());
        yangJobDTO.setCreateTime(yangJobModel.getCreateTime());
        yangJobDTO.setUpdateTime(yangJobModel.getUpdateTime());
        yangJobDTO.setFeatureMap(yangJobModel.getFeatureMap());
        yangJobDTO.setExecuteParamMap(yangJobModel.getExecuteParamMap());
        yangJobDTO.setExecuteStrategy(yangJobModel.getExecuteStrategy().getName());
        yangJobDTO.setExecuteClassPath(yangJobModel.getExecuteClassPath());
        if (yangJobModel.getJobType() == null) {
            yangJobDTO.setJobType(JobTypeEnum.LOCAL.getName());
            return yangJobDTO;
        }
        yangJobDTO.setJobType(yangJobModel.getJobType().getName());
        if (yangJobModel.getJobType() == JobTypeEnum.REMOTE && yangJobModel.getRemoteExecutorMessage() != null) {
            RemoteExecutorMessage remoteExecutorMessage = yangJobModel.getRemoteExecutorMessage();
            yangJobDTO.getExecuteParamMap().put(JobModelFeatureEnum.REMOTE_EXECUTOR_IP.getName(), remoteExecutorMessage.getIp());
            yangJobDTO.getExecuteParamMap().put(JobModelFeatureEnum.REMOTE_EXECUTOR_PORT.getName(), remoteExecutorMessage.getPort().toString());
        }
        return yangJobDTO;
    }
}
